﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Xml;
using System.Xml.Serialization;

namespace SusyGame_Model
{
    [Serializable]
    public class WaveManager
    {
        private int numberOfWaves; //Numero de olas que tendra el juego
        private float timeSinceLastWave; //Cuanto ha pasado desde que la ultima ola termino
        private Queue<Wave> waves = new Queue<Wave>(); //Limite para las olas
        private Texture2D enemyTexture;
        private bool waveFinished = false; //Indicara si la ola ha terminado
        private Map map;

        public Wave CurrentWave
        {
            get { return waves.Peek(); }
        }

        public List<Enemy> Enemies
        {
            get { return CurrentWave.Enemies; }
        }

        public int Round
        {
            get { return CurrentWave.WaveNumber + 1; }
        }

        public WaveManager()
        {
        }

        public WaveManager(Map map, int numberOfWaves, Texture2D enemyTexture)
        {
            this.numberOfWaves = numberOfWaves;
            this.enemyTexture = enemyTexture;

            this.map = map;

            for (int i = 0; i < numberOfWaves; i++)
            {
                int initialNumerOfEnemies = 36;
                int numberModifier = (i / 6) + 1;

                Wave wave = new Wave(i, initialNumerOfEnemies *
                    numberModifier, map, enemyTexture);

                waves.Enqueue(wave);
            }
            //StartNextWave();
        }

        public void StartNextWave()
        {
            if (waves.Count > 0) //Verifica si aun hay olas que faltan
            {
                waves.Peek().Start(); //Empezar una nueva

                timeSinceLastWave = 0; //Reinicia el tiempo
                waveFinished = false;
            }
        }

        public void Update(GameTime gameTime, Player player)
        {
            CurrentWave.Update(gameTime, player);

            if (CurrentWave.RoundOver) //Verifica si ha terminado
            {
                waveFinished = true;
            }

            if (waveFinished)
            {
                timeSinceLastWave += (float)gameTime.ElapsedGameTime.TotalSeconds; //Incia el tiempo
            }

            if (timeSinceLastWave > 10.0f)
            {
                waves.Dequeue(); //Quita la ola que ha terminado
                StartNextWave(); //Empieza una nueva ola
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            CurrentWave.Draw(spriteBatch);
        }
    }
}
